package com.detector.web.detector;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.detector.common.Pages;
import com.detector.entity.detector.DetectorInfo;
import com.detector.service.detector.DetectorInfoService;
import com.detector.service.org.InterfaceJson;

@Controller
@RequestMapping(value = "/detector/detectorInfo")
public class DetectorInfoController {

	private final Logger logger = LoggerFactory.getLogger(DetectorInfoController.class);
	@Resource
	private DetectorInfoService detectorInfoService;
	@Resource
	private InterfaceJson interfaceJson;

	/**
	 * 查询
	 * 
	 * @param DetectorInfo
	 * @param pages
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/listPage")
	public String listPage(DetectorInfo detectorInfo, Pages pages) {
		logger.debug("/detector/DetectorInfo/listPage is executed!");
		PageInfo<DetectorInfo> list = null;
		try {
			list = detectorInfoService.listPage2(detectorInfo, pages);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return interfaceJson.setErrorResult("/detector/DetectorInfo/listPage" + e.getMessage(), list);
		}
		return interfaceJson.setSuccessResult("成功", list);
	}

	@ResponseBody
	@RequestMapping(value = "/itemjson")
	public String itemjson() {
		logger.debug("/detector/DetectorInfo/itemjson is executed!");
		List<DetectorInfo> list = null;
		StringBuilder sb = new StringBuilder();

		try {
			list = detectorInfoService.list(null);
			if (list != null && !list.isEmpty()) {
				sb.append("[");
				for (int i = 0; i < list.size(); i++) {
					sb.append("[" + list.get(i).getCateName() + ",\"地址：北京市东城区王府井大街88号乐天银泰百货八层\"]");
					sb.append(",");
				}
				sb.deleteCharAt(sb.length() - 1);
				sb.append("]");
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			return interfaceJson.setErrorResult("/detector/DetectorInfo/list" + e.getMessage(), list);
		}

		return interfaceJson.setSuccessResult("成功", sb.toString());
	}

	/**
	 * 获取list数据
	 * 
	 * @param DetectorInfo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list")
	public String list(DetectorInfo detectorInfo) {
		logger.debug("/detector/DetectorInfo/list is executed!");
		List<DetectorInfo> list = null;
		try {
			list = detectorInfoService.list(detectorInfo);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return interfaceJson.setErrorResult("/detector/DetectorInfo/list" + e.getMessage(), list);
		}
		return interfaceJson.setSuccessResult("成功", list);
	}

	/**
	 * 插入
	 * 
	 * @param DetectorInfo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/insert")
	public String insert(DetectorInfo detectorInfo) {
		logger.debug("/detector/DetectorInfo/insert is executed!");
		DetectorInfo DetectorInfo1 = null;
		try {
			DetectorInfo1 = detectorInfoService.insertSelective(detectorInfo);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return interfaceJson.setErrorResult("/detector/DetectorInfo/insert" + e.getMessage(), DetectorInfo1);
		}
		return interfaceJson.setSuccessResult("成功", DetectorInfo1);
	}

	/**
	 * 修改
	 * 
	 * @param DetectorInfo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update")
	public String update(DetectorInfo detectorInfo) {
		logger.debug("/detector/DetectorInfo/update is executed!");
		DetectorInfo DetectorInfo1 = null;
		try {
			DetectorInfo1 = detectorInfoService.updateSelective(detectorInfo);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return interfaceJson.setErrorResult("/detector/DetectorInfo/update" + e.getMessage(), DetectorInfo1);
		}
		return interfaceJson.setSuccessResult("成功", DetectorInfo1);
	}

	/**
	 * 删除一条数据
	 * 
	 * @param DetectorInfo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/del")
	public String del(Long cateId) {
		logger.debug("/detector/DetectorInfo/del is executed!");
		try {
			detectorInfoService.deleteByCateId(cateId);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return interfaceJson.setErrorResult("/detector/DetectorInfo/del" + e.getMessage());
		}
		return interfaceJson.setSuccessResult("成功");
	}

}